//
//  UserProxy.m
//  DialectShare
//
//  Created by timmy zhong on 3/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UserProxy.h"
#import "PathHelper.h"
#import "ASIHTTPRequest.h"
#import "User.h"

@interface UserProxy ()
- (void)backgroundLogin:(User *)user;
- (void)backgroundFollow:(User *)user;
- (void)backgroundUnfollow:(User *)user;
- (void)backgroundRegister:(User *)user;
@end

@implementation UserProxy

- (void)login:(User *)user
{
    if ([self connectToNetwork]) {
        [self performSelectorInBackground:@selector(backgroundLogin:) withObject:user];
    }
}

- (void)backgroundLogin:(User *)user
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *url = [[PathHelper baseUrl] stringByAppendingFormat:@"c=user&a=login&account=%@&psw=%@", user.account, user.password];
    NSString *data = [self fetchDataForUrlString:url];
    
    NSDictionary *rtn = [self parse:data];
    if ([[rtn objectForKey:@"st"] intValue] == ServerStatusSuccess) {
        
        User *user = [User userWithDic:[rtn objectForKey:@"rs"]];
        [User store:user];
        
        [self.delegate performSelectorOnMainThread:@selector(requestFinishWithProxy:) withObject:self waitUntilDone:YES];
    }
    
    [pool release];
}

- (void)follow:(User *)user
{
    if ([self connectToNetwork]) {
        [self performSelectorInBackground:@selector(backgroundFollow:) withObject:user];
    }
}
- (void)backgroundFollow:(User *)user
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *url = [[PathHelper baseUrl] stringByAppendingFormat:@"c=user&a=follow&uid=%d", user.uid];
    NSString *data = [self fetchDataForUrlString:url];
    
    NSDictionary *rtn = [self parse:data];
    if ([[rtn objectForKey:@"st"] intValue] == ServerStatusSuccess) {
        
        [self.delegate performSelectorOnMainThread:@selector(requestFinishWithProxy:) withObject:self waitUntilDone:YES];
    }
    
    [pool release];
}

- (void)unfollow:(User *)user
{
    if ([self connectToNetwork]) {
        [self performSelectorInBackground:@selector(backgroundUnfollow:) withObject:user];
    }
}
- (void)backgroundUnfollow:(User *)user
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *url = [[PathHelper baseUrl] stringByAppendingFormat:@"c=user&a=unfollow&uid=%d", user.uid];
    NSString *data = [self fetchDataForUrlString:url];
    
    NSDictionary *rtn = [self parse:data];
    if ([[rtn objectForKey:@"st"] intValue] == ServerStatusSuccess) {
        
        [self.delegate performSelectorOnMainThread:@selector(requestFinishWithProxy:) withObject:self waitUntilDone:YES];
    }
    
    [pool release];
}

- (void)registerUser:(User *)user
{
    if ([self connectToNetwork]) {
        [self performSelectorInBackground:@selector(backgroundRegister:) withObject:user];
    } else {
        [self.delegate requestFinishWithProxy:self];
    }
}

- (void)backgroundRegister:(User *)user
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *url = [[PathHelper baseUrl] stringByAppendingFormat:@"c=user&a=reg"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:user.account, @"account", user.password, @"psw", user.name, @"name", nil];
    NSString *data = [self postData:dic files:nil url:url];
    NSDictionary *rtn = [self parse:data];
    if ([[rtn objectForKey:@"st"] intValue] == ServerStatusSuccess) {
        
        User *user = [User userWithDic:[rtn objectForKey:@"rs"]];
        [User store:user];
        
        [self.delegate performSelectorOnMainThread:@selector(requestFinishWithProxy:) withObject:self waitUntilDone:YES];
    } else {
        
        self.object = [rtn objectForKey:@"msg"];
        [self.delegate performSelectorOnMainThread:@selector(requestFailWithProxy:) withObject:self waitUntilDone:YES];
    }
    
    [pool release];
}














@end
